﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using WPFExcelReport.ViweModels;
namespace WPFExcelReport
{
    /// TBList="ICC_SRULES;HRSTAMM;ICC_HRSTAMM;ICC_FOLDERS;ICC_FUNCTIONS;ICC_MENUDEF;ICC_MULLANG;ICC_GABBS;ICC_WFEVENTDEF;ICC_WFEVENTSCLASS;ICC_WFEVENTSGROUP;ICC_WFSUBSCRIBER;";//ITXSETUP
    ///
    public partial class ITXSETUP : DocumentBase
    {
        VKSLEIST S;
        VICC_FOLDERS fd;
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        bool IsLoaded = false;
        ITXEXRPT ITXEXRPT;
       
        public SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        public SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }   
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string Mandant
        {
            get
            {
                return vm.Mandant;
            }
        }
        public string company
        {
            get
            {
                return vm.company;
            }
        }
        MainViewModel vm;
        public ITXSETUP()
        {
            InitializeComponent();
            this.Name = "参数设定"; 
        }
      
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded) return;
                IsLoaded = true;
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                } 
                vm = new MainViewModel();
                vm.dicParamater = dicParamater;
                this.DataContext = vm;
                SetEnv.Merge(vm.LoadEnv(Mandant));
             }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    this.dicParamater = dicParamater;
                }
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void butDiscard_Click(object sender, RoutedEventArgs e)
        {
            SetDetail.Clear();
            SetDetail.AcceptChanges();
        }
        DataSet SetExcel = new System.Data.DataSet();
        private void btnExDataSave_Click(object sender, RoutedEventArgs e)
        {
            foreach (DataTable tb in SetExcel.Tables)
            {
                if (tb.Rows.Count == 0)
                    continue;
                foreach (DataRow row in tb.Rows)
                {
                    INTEXDBC.InsertAnyTable(row, "ITX");
                }
            }

        }

        private void btnExDataLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                ITXEXRPT.DocType = "LOADEXCEL";
                ITXEXRPT.WriteExcelReport();
                Result = ITXEXRPT.SetExchange;
                if (Result.Tables.Count > 0)
                {
                    txtInfo.Text = Result.Tables[0].Rows.Count.ToString() + "行数据已经读入";
                    SetExcel.Clear();
                    SetExcel.Merge(Result);
                }
                else
                {
                    MessageBox.Show("确认存在"+@"C:\IntexReport\Data\ITX\EXCEL\ITXEX-XXXX.xls");
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        } 
        
        VICC_MENUDEF MU;
        VICC_WFEVENTDEF WFD;
        VICC_WFEVENTSCLASS WFC;
        VICC_WFEVENTSGROUP WFG;
        private void MDCommand(object sender, RoutedEventArgs e)
        {
            MU = new VICC_MENUDEF();
            MU.dwData = MU.TBMain.NewRow();
            MU.SetUpNewRow(ref MU.dwData);
            MU.dwData["COMPANY"] = cMD.Text.Trim();
            MU.InsertICC_MENUDEF(MU.dwData, app.company); 
        } 
    }
}
